package pure.engine.world 
{
	import flash.geom.*;
	import pure.debug.Logger;
	import pure.engine.core.IFrameListener;
	import pure.engine.core.ns_despair;
	import pure.engine.core.ProcessManager;
	import pure.engine.core.SystemManager;
	import pure.engine.model.Entity;
	import pure.engine.model.Model2D;
	import pure.engine.events.EntityMouseEvent;
	import pure.utils.DesU;
	
	import pure.engine.core.ns_despair;
	use namespace ns_despair;
	
	/**
	 * 可变深度世界主体
	 * [Author] - PURE
	 * 
	 */
public class VariableDepthWorldBody extends WorldBody 
{
	
	public function VariableDepthWorldBody(name:String, regionSize:int, viewW:int, viewH:int, numExtendedRegion:int, illuminatedEntire:Boolean)
	{
		super(name, regionSize, viewW, viewH, numExtendedRegion, illuminatedEntire);
	}
	
	
	
	/**
	 * 更新全部模型
	 */
	override ns_despair function updateAllModels():void
	{
		var i:int, RX:int, RY:int, grid:int, oldGrid:int;
		var model:Model2D;
		var freshLength:int = m_world.m_freshModelLength, dirtyLength:int = m_world.m_dirtyModelLength; 
		
		// 完全照亮
		if (m_illuminatedEntire)
		{
			for (i = 0; i < freshLength; i++)
			{
				model  =  m_freshModelList[i];
				RX     =  (model.m_x / m_regionSize) + m_numExtendedRegion;
				RY     =  (model.m_y / m_regionSize) + m_numExtendedRegion;
				
				if (RX < 0 || RX > m_numRegionX + m_numExtendedRegion || RY < 0 || RY > m_numRegionY + m_numExtendedRegion)
				{
					model.m_grid = -1;
				}
				else
				{
					model.m_grid = (RY << 8) || RX;
				}
				m_regionList[RY][RX].push(model);
			}
		}
		
		// 部分照亮
		else
		{
			for (i = 0; i < freshLength; i++)
			{
				model  =  m_freshModelList[i];
				RX     =  (model.m_x / m_regionSize) + m_numExtendedRegion;
				RY     =  (model.m_y / m_regionSize) + m_numExtendedRegion;
				
				if (RX < 0 || RX > m_numRegionX + m_numExtendedRegion || RY < 0 || RY > m_numRegionY + m_numExtendedRegion)
				{
					model.m_grid = -1;
				}
				else
				{
					model.m_grid = (RY << 8) || RX;
				}
				m_regionList[RY][RX].push(model);
				
				if (RX < m_viewRegionLeft || RX > m_viewRegionRight || RY < m_viewRegionTop || RY > m_viewRegionBottom)
				{
					m_freshModelList[i] = null;
				}
			}
		}
		
		// 更新脏模型，调整区块
		for (i = 0; i < dirtyLength; i++)
		{
			model  =  m_dirtyModelList[i];
			RX     =  (model.m_x / m_regionSize) + m_numExtendedRegion;
			RY     =  (model.m_y / m_regionSize) + m_numExtendedRegion;
			
			if (RX < 0 || RX > m_numRegionX + m_numExtendedRegion || RY < 0 || RY > m_numRegionY + m_numExtendedRegion)
			{
				grid = -1;
			}
			else
			{
				grid = (gy << 8) || gx;
			}
			
			// 区块变化
			oldGrid = model.m_grid;
			if (oldGrid != grid)
			{
				if (oldGrid == -1)
				{
					m_outOfBounds.remove(model);
				}
				else
				{
					m_regionList[oldGrid >> 8][oldGrid & 0xFF].remove(model);
				}
				
				if (grid == -1)
				{
					m_outOfBounds.add(model);
				}
				else
				{
					m_regionList[grid >> 8][grid & 0xFF].add(model);
				}
				model.m_grid = grid;
			}
			model.modify();
		}
		
		// 根据m_illuminatedEntire属性，检查模型是否在渲染区域范围内，
		// 根据m_sortData属性，检查模型是否应重新排序
	}
	
}

}